<template>
  <el-container class="home-container">
    <el-header>
      <div >
        <img src="../assets/logo.png"/>
        <span>道路资产管理系统</span>
      </div>

      <div>
         <el-button type="info" @click="logout()">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside  :width="isCollapse ? '64px':'200px'" >
        <div class="toggle-button" @click="toggleCollapse">||||</div>
        <el-menu
              :default-active="activePath"
              class="el-menu-vertical-demo"
              @open="handleOpen"
              @close="handleClose"
              background-color="#333744"
              text-color="#fff"
              :unique-opened="false"
              :collapse="isCollapse"
              :router="true"
              :collapse-transition="false"
              active-text-color="#409eff">

              <el-submenu :index="item.path+''" v-for="item in menulist" :key="item.id">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item.authName}}</span>
                </template>

                  <el-menu-item :index="'/'+sub.path" v-for="sub in item.children" :key="sub.id" @click="saveNavState('/'+sub.path)">
                    <template slot="title">
                      <i class="el-icon-menu"></i>
                      <span>{{sub.authName}}</span>
                    </template>
                  </el-menu-item>
              </el-submenu>

            </el-menu>
      </el-aside>

      <el-main>
        <!-- 路由占位符 -->
        <router-view>

        </router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      isCollapse: false,
      // 菜单选中项
      activePath: ''

    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menulist = res.data
      }
    },
    // 切换菜单折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 30px;
    > div {
      height: 60px;
      padding: 0px;
      display: flex;
      align-items: center;
      img {
        height: 50px;
        width: 50px;
      }
      span{
        margin-left: 15px;
      }
    }

  }

  .el-aside{
    background-color: #333744;
    color: #fff;
    .el-menu {
      border-right: none;
    }
  }

  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-main{
    background-color: #eaedf1;
  }
</style>
